package com.zzc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzc.common.JsonResult;
import com.zzc.common.TableResult;
import com.zzc.mapper.AppInfoMapper;
import com.zzc.mapper.CustomerMapper;
import com.zzc.mapper.UserTokenMapper;
import com.zzc.pojo.AppInfo;
import com.zzc.pojo.UserToken;
import com.zzc.service.AppInfoService;
import com.zzc.service.CustomerService;
import com.zzc.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhouzhicheng
 * @version 1.0  2020/12/29
 */
@Service
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public TableResult getAppInfoList(String corpName,Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        PageInfo<AppInfo> pageInfo = new PageInfo<>(appInfoMapper.getAppInfoList(corpName));
        return new TableResult(0,null,Long.valueOf(pageInfo.getTotal()).intValue(), pageInfo.getList());
    }

    @Override
    public JsonResult deleteAppInforById(Integer[] ids) {
        if (ids==null){
            return JsonResult.getResultFailed("参数有误！");
        }
        for (Integer id : ids) {
            appInfoMapper.deleteByPrimaryKey(id);
        }
        return JsonResult.getResultSuccess();
    }

    @Override
    public JsonResult insertAppInfor(AppInfo appInfo) {
        if (appInfo==null){
            return JsonResult.getResultFailed("参数有误！");
        }
        //根据客户id查询公司名
        String cropName = customerMapper.selectByPrimaryKey(appInfo.getCusId()).getUsername();
        appInfo.setCorpName(cropName);
        System.out.println(appInfo);
        return appInfoMapper.insertSelective(appInfo)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("添加失败！");
    }

    @Override
    public JsonResult updateByPrimaryKeySelective(AppInfo appInfo) {
        if (appInfo==null){
            return JsonResult.getResultFailed("参数有误！");
        }
        //根据客户id查询公司名
        String cropName = customerMapper.selectByPrimaryKey(appInfo.getCusId()).getUsername();
        appInfo.setCorpName(cropName);
        return appInfoMapper.updateByPrimaryKeySelective(appInfo)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("修改失败！");
    }
}
